<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\UserGender;
use App\Models\Movie;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use \Illuminate\Http\Request;

class MovieController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = 'Movies';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {

        $grid = new Grid(new Movie);
        $grid->column('id', 'ID')->sortable();
        $grid->column('title');
        $grid->column('director')->display(function($userId){
            return  User::find($userId)->name;
        });
        $grid->column('describe');
        $grid->column('rate');
        $grid->column('released', '上映')->display(function($released) {
            return $released ? '是': '否';
        });
        $grid->column('release_at')->filter('range', 'date');
        $grid->column('created_at');
        $grid->column('updated_at');


        $grid->tools(function ($tools) {
            $tools->append(new UserGender());

        });

        $grid->filter(function($filter) {
            $filter->expand();
//            $filter->disableIdFilter();

/*
            $filter->between('created_at', '创建日期')->datetime();
//            $filter->equal('title', '标题');
//            $filter->notEqual('title', '标题排除');
            $filter->like('title', '标题模糊查询');*/

            $filter->column(0.5, function($filter) {
                $filter->like('title');
                $filter->between('rate');
            });

            $filter->column(1/2, function($filter) {
                $filter->like('created_at')->datetime();
                $filter->between('updated_at')->datetime();
                $filter->equal('released')->radio([
                    1=> 'YES',
                    0 => 'NO',
                ]);
            });




        });

        $grid->export(function($export) {
            $export->except(['created_at','updated_at']);
        });


        //表头，列的筛选
//        $grid->column('director','价格')->filter('range');


        $grid->actions(function($actions) {
            $actions->disableDelete();

        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(User::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('email', __('Email'));
        $show->field('email_verified_at', __('Email verified at'));
        $show->field('password', __('Password'));
        $show->field('remember_token', __('Remember token'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new User());

        $form->text('name', __('Name'));
        $form->email('email', __('Email'));
        $form->datetime('email_verified_at', __('Email verified at'))->default(date('Y-m-d H:i:s'));
        $form->password('password', __('Password'));
        $form->text('remember_token', __('Remember token'));



        return $form;
    }
}
